package com.evon.yardmanagement.ui.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.ApplicationAware;

import com.evon.yardmanagement.model.Transporter;
import com.evon.yardmanagement.model.Yard;
import com.evon.yardmanagement.service.MemberService;
import com.evon.yardmanagement.service.OEMService;
import com.evon.yardmanagement.service.TransporterService;
import com.evon.yardmanagement.service.YardManagementService;
import com.evon.yardmanagement.transformer.BaseTransformer;
import com.evon.yardmanagement.transformer.TransporterTransformer;
import com.evon.yardmanagement.ui.dto.MemberDTO;
import com.evon.yardmanagement.ui.dto.OemDTO;
import com.evon.yardmanagement.ui.dto.TransporterDTO;
import com.opensymphony.xwork2.ActionSupport;

public class ManageTransporterAction extends ActionSupport implements
		ApplicationAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	BaseTransformer<Transporter, TransporterDTO> transformer = new BaseTransformer<Transporter, TransporterDTO>(
			Transporter.class, TransporterDTO.class);
	Map<String, Object> globalData;

	private List<TransporterDTO> listTransporter = new ArrayList<TransporterDTO>();

	public List<TransporterDTO> getListTransporter() {
		return listTransporter;
	}

	public void setListTransporter(List<TransporterDTO> listTransporter) {
		this.listTransporter = listTransporter;
	}

	private int selectedYardId;
	public List<Yard> listYard = new ArrayList<Yard>();
    private List<OemDTO> oemList;
	
	private int oemType;
	
	public List<OemDTO> getOemList() {
		return oemList;
	}

	public void setOemList(List<OemDTO> oemList) {
		this.oemList = oemList;
	}

	public int getOemType() {
		return oemType;
	}

	public void setOemType(int oemType) {
		this.oemType = oemType;
	}

	public String init() {
		oemList = OEMService.getAllOem();
		//Addded for back button
		if (getSelectedYardId()!=0) {
			listYard = new YardManagementService().getYardsByOemId(oemType);	
			List<Transporter> transporters = new TransporterService().getTransporterList(selectedYardId);
	        listTransporter =  transformer.transformToDTO(transporters);
			globalData.put("YARDS", listYard);
		}
		//end
		globalData.put("oemList", oemList);
		return SUCCESS;
	}
	
	public String populateYard() {
		oemList = (List<OemDTO>)globalData.get("oemList");
		listYard = new YardManagementService().getYardsByOemId(oemType);
		globalData.put("YARDS", listYard);
		return SUCCESS;
	}

	public String execute() {
		System.err.println("addsds" + selectedYardId);
		listYard = (List<Yard>) globalData.get("YARDS");
		List<Transporter> transporters = new TransporterService()
				.getTransporterList(selectedYardId);
		listTransporter =  transformer.transformToDTO(transporters);
		//TransporterTransformer.transform(transporters);
		return SUCCESS;
	}

	public int getSelectedYardId() {
		return selectedYardId;
	}

	public void setSelectedYardId(int selectedYardId) {
		this.selectedYardId = selectedYardId;
	}

	public void setApplication(Map<String, Object> application) {
		globalData = application;

	}

}
